/*
 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "board.h"
#include <stdio.h>
#include <stdlib.h>
#include "asr_rv32.h"
#include "asr_pinmux.h"
#include "asr_uart.h"
#include "printf_uart.h"
#include "asr_rtc.h"

#if 0
#include "los_config.h"
#include "los_debug.h"
#include "los_interrupt.h"
#include "los_task.h"
#include "los_tick.h"
#else
#include "cmsis_os.h"
#include "lega_at_api.h"
#endif
#include "los_exc_info.h"
#include "alto_version.h"
#include "asr_gpio.h"
#include "iot_gpio.h"

/*
main task stask size(byte)
*/
#define OS_MAIN_TASK_STACK (4096)

osThreadAttr_t g_main_task={"main_task",0,NULL,0,NULL,OS_MAIN_TASK_STACK,15,0,0};
osThreadAttr_t g_rtc_task={"rtc_task",0,NULL,0,NULL,OS_MAIN_TASK_STACK,15,0,0};




extern void HalPendSV(void);
void PendSV_Handler(void)
{
    HalPendSV();
}
#ifdef MQTTV5_DEMO
extern void mqttv5_demo_init(void);
#endif
static void sys_init(void)
{

#ifdef RELEASE_DOMAIN
    //LOG("log_disable");
    lega_log_disable();
#endif
    alto_flash_kv_init();

    board_after_init();
    //debug_memory_access_err_check(0x0, 0x10000, MPU_AP_NA_NA);

#ifdef SYSTEM_COREDUMP
    //coredump_command_register(0, NULL);
#endif

#ifdef CFG_DUAL_AP
    comm_wifi_command_register(0,NULL);
#endif

    lega_at_init(AT_TASK_NAME,AT_TASK_PRIORITY,AT_TASK_STACK_SIZE);
    lega_at_cmd_register_all();

#ifdef ASR_PROFILE_ON
    asr_profile_config();
#endif

#ifdef LWIP_APP_IPERF
    lega_wifi_iperf_init();
#endif
#ifdef MQTTV5_DEMO
    mqttv5_demo_init();
#endif
#ifdef BLE_APP_AT_CMD
    extern void lega_ble_at_init(void);
    lega_ble_at_init();
#endif

#ifdef CFG_MRFOTA_TEST
    //extern void lega_rfota_wifi_test_at_init(void);
    //lega_rfota_wifi_test_at_init();
    //extern void lega_rfota_ble_test_at_init(void);
    //lega_rfota_ble_test_at_init();
#endif
    printf("sys_init running...\n");
}

asr_rtc_dev_t rtc_dev = {0};

asr_rtc_time_t rtc_time = {0};

void print_date(asr_rtc_time_t *rtc_time)
{
    printf("date is: %d %d %d %d %d %d %d\r\n",rtc_time->year+1900,rtc_time->month,rtc_time->date, \
    rtc_time->hr,rtc_time->min,rtc_time->sec,rtc_time->weekday);
}

void rtc_init(void)
{
    rtc_dev.port = 0;
    asr_rtc_finalize(&rtc_dev);
    asr_rtc_init(&rtc_dev);
    asr_rtc_get_time(&rtc_dev, &rtc_time);
    print_date(&rtc_time);

    rtc_time.year = RTC_INIT_YEAR;
    rtc_time.month = RTC_INIT_MONTH;
    rtc_time.date = RTC_INIT_DATE;
    rtc_time.hr = RTC_INIT_HOUR;
    rtc_time.min = RTC_INIT_MINUTE;
    rtc_time.sec = RTC_INIT_SECOND;

    asr_rtc_set_time(&rtc_dev, &rtc_time);
    asr_rtc_get_time(&rtc_dev, &rtc_time);
    print_date(&rtc_time);
}


void rtc_test(void){
	printf("rtc_test.....\r\n");
	rtc_init();
}

int main(void)
{
    osStatus_t ret;
    osThreadId_t threadId;
    osThreadId_t rtctest;
    board_before_init();
    //board_uart_init();

    board_init();
    board_uart_init();
    printf("\r\n version app:%s\r\n", LEGA_VERSION_STR);
#if LOSCFG_PLATFORM_EXC == 1
    OsExcMsgDumpInit();
#endif

#ifdef DEBUG_BACKTRACE
    debug_backtrace_init();
#endif // DEBUG_BACKTRACE
    ret = osKernelInitialize();

    if(ret == osOK)
    {
        threadId = osThreadNew((osThreadFunc_t)sys_init,NULL,&g_main_task);
		rtctest = osThreadNew((osThreadFunc_t)rtc_test,NULL,&g_rtc_task);
        if(threadId!=NULL)
        {

            osKernelStart();
        }
		
    }   

    while(1);    
    return 0;
}
